package com.xinqi.modules.exam.analyze.dto;

import com.xinqi.common.base.enums.QuestionTypeEnum;
import com.xinqi.common.base.utils.number.BigDecimals;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

import java.math.BigDecimal;

/**
 * 试题得分率
 *
 * @author linkai
 * @date 2023/7/16
 */
@Data
@Schema(description = "课程测评分析-试题得分率")
public class QuestionScoreRateDTO {
    /**
     * 题目ID
     */
    @Schema(description = "题目ID", example = "")
    private Long questionId;
    /**
     * 题目排序
     */
    @Schema(description = "题目排序", example = "")
    private Integer sort;
    /**
     * 题目名称
     */
    @Schema(description = "题目名称", example = "")
    private String questionName;
    /**
     * 题目类型
     */
    @Schema(description = "题目类型", example = "")
    private String questionType;
    /**
     * 参考人数
     */
    @Schema(description = "参考人数", example = "")
    private Integer totalNum;
    /**
     * 题目总分
     */
    @Schema(description = "题目总分", example = "")
    private BigDecimal totalScore;
    /**
     * 答对人数
     */
    @Schema(description = "答对人数", example = "")
    private Integer rightNum;
    /**
     * 答对分数
     */
    @Schema(description = "答对分数", example = "")
    private BigDecimal rightScore;
    /**
     * 题目得分率
     */
    @Schema(description = "题目得分率", example = "")
    private BigDecimal questionScoreRate;

    /**
     * 计算题目得分率， 答对分数 / 总分数
     */
    public void computeScoreRate() {
        if (rightScore == null || rightScore.compareTo(BigDecimal.ZERO) == 0 || totalScore == null || totalScore.compareTo(BigDecimal.ZERO) == 0) {
            questionScoreRate = BigDecimal.ZERO;
            return;
        }
        if (QuestionTypeEnum.isObjective(questionType)) {
            // 客观题得分率统计： 答对人数 / 参考人数
            questionScoreRate = BigDecimals.divide(rightNum, totalNum);
        } else {
            // 主观题得分率统计
            // 题目总分 * 参考人数
            BigDecimal t = BigDecimals.multiply(totalScore, totalNum);
            // 答对分数 / (题目总分 * 参考人数)
            questionScoreRate = BigDecimals.divide(rightScore, t);
        }

    }
}
